package com.example.langchain4j06chatimage.controller;

import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

/**
 * @author wwl
 * 日期  2025/9/22 17:14
 * 版本 1.0
 * 描述 :
 */
@RestController
@Slf4j
public class ImageModelController {
    @Autowired
    private ChatModel imageModel;
    @Value("classpath:static/images/mi.png")
    private Resource resource;

    @GetMapping(value = "/image/call")
    public String readImageContent()throws IOException {

        byte[] byteArray = resource.getContentAsByteArray();
        String base64Data = Base64.getEncoder().encodeToString(byteArray);

        UserMessage userMessage = UserMessage.from(
                TextContent.from("从以下图片中获取来源网站名称，股价走势和5月30号股价"),
                ImageContent.from(base64Data,"image/png")
        );

        ChatResponse chatResponse = imageModel.chat(userMessage);
        String result = chatResponse.aiMessage().text();

        System.out.println(result);

        return result;
    }

}
